﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Thor.Domain;

namespace Thor.Domain
{
    public class InvalidLogonManager
    {
        #region Fields

        private IInvalidLogonRepository _invalidLogonRepository = null;
        private List<InvalidLogon> _invalidLogons = null;

        #endregion

        #region Constructors

        public InvalidLogonManager(IInvalidLogonRepository invalidLogonRepository)
        {
            _invalidLogonRepository = invalidLogonRepository;
        }

        #endregion

        #region Properties

        /// <summary>
        /// List of people invalid logons.
        /// </summary>
        public List<InvalidLogon> InvalidLogons
        {
            get
            {
                if (_invalidLogons == null)
                    _invalidLogons = _invalidLogonRepository.Fetch();

                if (_invalidLogons == null)
                    _invalidLogons = new List<InvalidLogon>();

                return _invalidLogons;
            }
            set { _invalidLogons = value; }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Inserts an invalid logon.
        /// </summary>
        /// <param name="invalidLogon"></param>
        public void Insert(InvalidLogon invalidLogon)
        {
            if (invalidLogon.Id == Guid.Empty)
                invalidLogon.Id = Guid.NewGuid();

            _invalidLogonRepository.Insert(invalidLogon);
        }

        /// <summary>
        /// Updates an invalid logon.
        /// </summary>
        /// <param name="invalidLogon"></param>
        public void Update(InvalidLogon invalidLogon)
        {
            _invalidLogonRepository.Update(invalidLogon);
        }

        /// <summary>
        /// Deletes the invalid logon.
        /// </summary>
        /// <param name="invalidLogon"></param>
        public void Delete(InvalidLogon invalidLogon)
        {
            _invalidLogonRepository.Delete(invalidLogon);
        }

        /// <summary>
        /// Gets an invalid logon.
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public InvalidLogon GetInvalidLogon(string userName)
        {
            var invalidLogon = (from item in InvalidLogons
                                where item.UserName == userName
                                select item).FirstOrDefault();

            return invalidLogon;
        }

        /// <summary>
        /// Gets a concrete implementation of IInvalidLogonRepository.
        /// </summary>
        /// <param name="assemblyPath">Full path to the assembly.</param>
        /// <param name="typeName">Full type name of the type in the assembly that implements IInvalidLogonRepository.</param>
        /// <returns></returns>
        public static IInvalidLogonRepository GetRepository(string assemblyPath, string typeName, Dictionary<string, object> repositoryParameters)
        {
            //Load the assembly from the specified path.
            Assembly assembly = Assembly.LoadFile(assemblyPath);

            //Load the type
            Type type = assembly.GetType(typeName);

            //Get the constructor
            Type[] constructorTypes = new Type[1];
            constructorTypes[0] = typeof(Dictionary<string, object>);
            ConstructorInfo constructorInfo = type.GetConstructor(constructorTypes);

            //Invoke the constructor and return a new instance
            if (constructorInfo != null)
                return constructorInfo.Invoke(new object[] { repositoryParameters }) as IInvalidLogonRepository;

            return null;
        }

        #endregion
    }
}
